﻿using System;

namespace LxCFD
{
    public class FindError
    {
        public static void FindMaxAndMin(double[] array)
        {
            double max = array[0];
            double min = array[0];
            int maxIndex = 0;
            int minIndex = 0;
            for (int i1 = 1; i1 < array.Length; i1++)
            {
                if (array[i1] > max)
                {
                    max = array[i1];
                    maxIndex = i1;
                }
                else if (array[i1] < min)
                {
                    min = array[i1];
                    minIndex = i1;
                }
            }
            Console.WriteLine("max:{0} at {1}; min:{2} at {3}", max, maxIndex, min, minIndex);
        }
        public static void PrintAxb(object obj, int cellindex)
        {
            Console.WriteLine("Cell Index {0}", cellindex);
            if (obj is ScalarBase sx)
            {
                if (cellindex < sx.Grad.Length)
                {
                    Console.WriteLine("x= {0} ,gradx= {1} ", sx.x[cellindex], sx.Grad[cellindex]);
                }
                else
                {
                    Console.WriteLine("x= {0} ", sx.x[cellindex]);
                }
            }
            if (obj is MatrixADbBase sab)
            {
                Console.WriteLine("A_Diag= {0} ,b= {1} ", sab.ADiag[cellindex], sab.b[cellindex]);
            }
            if (obj is EquationBase flow)
            {
                FvCell[] cells = flow.Zone.Cells;
                FvFace[] faces = flow.Zone.Faces;
                foreach (var face in cells[cellindex].OFaces)
                {
                    Console.WriteLine("Oface Id= {0} ,ALUO= {1} xN= {2} ", face, flow.ALUO[face], flow.x[faces[face].NSideCell]);
                }
                foreach (var face in flow.Zone.Cells[cellindex].NFaces)
                {
                    Console.WriteLine("Nface Id= {0} ,ALUN= {1} xO= {2} ", face, flow.ALUN[face], flow.x[faces[face].OSideCell]);
                }
            }
            //else if (obj is ScalarBase_ALUDiff aludiff)
            //{
            //    Console.WriteLine("A_LU_Diff:");
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsO)
            //    {
            //        Console.WriteLine("Oface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsN)
            //    {
            //        Console.WriteLine("Nface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //}
        }
        public static void PrintAxb_x2common(object obj, int cellindex)
        {
            Console.WriteLine("Cell Index {0}", cellindex);
            if (obj is ScalarBase sx)
            {
                if (cellindex < sx.Grad.Length)
                {
                    Console.WriteLine("x2common,x= {0} ", sx.x[cellindex]);
                    Console.WriteLine("x= {0} ,gradx= {1} ", sx.x_Common[cellindex], sx.Grad[cellindex]);
                }
                else
                {
                    Console.WriteLine("x= {0} ", sx.x[cellindex]);
                }
            }
            if (obj is MatrixADbBase sab)
            {
                Console.WriteLine("A_Diag= {0} ,b= {1} ", sab.ADiag[cellindex], sab.b[cellindex]);
            }
            if (obj is EquationBase flow)
            {
                FvCell[] cells = flow.Zone.Cells;
                FvFace[] faces = flow.Zone.Faces;
                foreach (var face in cells[cellindex].OFaces)
                {
                    Console.WriteLine("Oface Id= {0} ,ALUO= {1} xN= {2} ", face, flow.ALUO[face], flow.x[faces[face].NSideCell]);
                }
                foreach (var face in flow.Zone.Cells[cellindex].NFaces)
                {
                    Console.WriteLine("Nface Id= {0} ,ALUN= {1} xO= {2} ", face, flow.ALUN[face], flow.x[faces[face].OSideCell]);
                }
            }
            //else if (obj is ScalarBase_ALUDiff aludiff)
            //{
            //    Console.WriteLine("A_LU_Diff:");
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsO)
            //    {
            //        Console.WriteLine("Oface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsN)
            //    {
            //        Console.WriteLine("Nface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //}
        }
        public static void PrintAxb_common2x(object obj, int cellindex)
        {
            Console.WriteLine("Cell Index {0}", cellindex);
            if (obj is ScalarBase sx)
            {
                if (cellindex < sx.Grad.Length)
                {
                    Console.WriteLine("common2x,x_common= {0} ", sx.x_Common[cellindex]);
                    Console.WriteLine("x= {0} ,gradx= {1} ", sx.x[cellindex], sx.Grad[cellindex]);
                }
                else
                {
                    Console.WriteLine("x= {0} ", sx.x[cellindex]);
                }
            }
            if (obj is MatrixADbBase sab)
            {
                Console.WriteLine("A_Diag= {0} ,b= {1} ", sab.ADiag[cellindex], sab.b[cellindex]);
            }
            if (obj is EquationBase flow)
            {
                FvCell[] cells = flow.Zone.Cells;
                FvFace[] faces = flow.Zone.Faces;
                foreach (var face in cells[cellindex].OFaces)
                {
                    Console.WriteLine("Oface Id= {0} ,ALUO= {1} xN= {2} ", face, flow.ALUO[face], flow.x_Common[faces[face].NSideCell]);
                }
                foreach (var face in flow.Zone.Cells[cellindex].NFaces)
                {
                    Console.WriteLine("Nface Id= {0} ,ALUN= {1} xO= {2} ", face, flow.ALUN[face], flow.x_Common[faces[face].OSideCell]);
                }
            }
            //else if (obj is ScalarBase_ALUDiff aludiff)
            //{
            //    Console.WriteLine("A_LU_Diff:");
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsO)
            //    {
            //        Console.WriteLine("Oface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //    foreach (var face in aludiff.zone.cells[cellindex].CellFacesAsN)
            //    {
            //        Console.WriteLine("Nface Id= {0},ALU={1}", face, aludiff.A_LU_Diff[face]);
            //    }
            //}
        }
        public static void FindNAN(double[] target)
        {
            for (int i1 = 0; i1 < target.Length; i1++)
            {
                if (double.IsNaN(target[i1])) throw new Exception("NAN");
            }
        }
        public static bool FindNaN(double[] target)
        {
            for (int i1 = 0; i1 < target.Length; i1++)
            {
                if (double.IsNaN(target[i1])) return true;
            }
            return false;
        }
        public static bool FindUnnormal(double[] target)
        {
            for (int i1 = 0; i1 < target.Length; i1++)
            {
                if (double.IsNaN(target[i1]) || double.IsInfinity(target[i1]) || double.IsNegativeInfinity(target[i1])) return true;
            }
            return false;
        }


    }
}
